package game.games.gol;

import game.coord.MooreCell;

import java.util.Arrays;
import java.util.List;

public class Cell extends MooreCell<Cell>
{
    private boolean alive;
    private boolean surviving;
    
    public Cell()
    {
        this(false, false);
    }
    
    public Cell(boolean alive, boolean surviving)
    {
        this.setAlive(alive);
        this.setSurviving(surviving);
    }
    
    public boolean isAlive()
    {
        return alive;
    }
    
    public void setAlive(boolean alive)
    {
        this.alive = alive;
    }
    
    public boolean isSurviving()
    {
        return surviving;
    }
    
    public void setSurviving(boolean surviving)
    {
        this.surviving = surviving;
    }
    
    public int countLivingNeighbors()
    {
        int livingNeighbors = 0;
        if (getTopLeft().isAlive())
        {
            livingNeighbors++;
        }
        if (getTop().isAlive())
        {
            livingNeighbors++;
        }
        if (getTopRight().isAlive())
        {
            livingNeighbors++;
        }
        if (getLeft().isAlive())
        {
            livingNeighbors++;
        }
        if (getRight().isAlive())
        {
            livingNeighbors++;
        }
        if (getBottomLeft().isAlive())
        {
            livingNeighbors++;
        }
        if (getBottom().isAlive())
        {
            livingNeighbors++;
        }
        if (getBottomRight().isAlive())
        {
            livingNeighbors++;
        }
        return livingNeighbors;
    }

    public List<Cell> getNeighbors()
    {
        return Arrays.asList(new Cell[] {
                getTopLeft(),
                getTop(),
                getTopRight(),
                getLeft(),
                getRight(),
                getBottomLeft(), 
                getBottom(),
                getBottomRight()
        });
    }
    
    @Override
    public String toString()
    {
        StringBuilder s = new StringBuilder();
        s.append("alive=").append(isAlive());
        s.append("; surviving=").append(isSurviving());
        return s.toString();
    }
}
